package cqcvc.edu.cn.dao;

import cqcvc.edu.cn.base.BaseDao;
import cqcvc.edu.cn.entity.HouseInfo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class HouseDao {
    /**
     * 返回房型信息数据
     * @return 房型信息数据集
     */
    public List<HouseInfo> findAll()
    {
        Connection conn= BaseDao.getConn();
        try {
            Statement statement = conn.createStatement();
            String sql = "select * from house_info ";
            ResultSet rs = statement.executeQuery(sql);
            return getList(rs);
        }catch (Exception ex)
        {
            return null;
        }
    }

    public HouseInfo findById(Long id)
    {
        Connection conn= BaseDao.getConn();
        try {
            Statement statement = conn.createStatement();
            String sql = "select * from house_info where id="+id;
            ResultSet rs = statement.executeQuery(sql);
            List<HouseInfo> his= getList(rs);
            if(his.size()>0)
            {
                return his.get(0);
            }
        }catch (Exception ex)
        {
            return null;
        }
        return null;
    }

    private List<HouseInfo> getList(ResultSet rs)
    {
        List<HouseInfo> hiList=new ArrayList<>();
        try
        {
            while (rs.next())
            {
                HouseInfo item=new HouseInfo();
                item.setCreateDate(rs.getDate("create_date"));
                item.setHouseDetail(rs.getString("house_detail"));
                item.setHousePrice(rs.getFloat("house_price"));
                item.setHouseTitle(rs.getString("house_title"));
                item.setId(rs.getLong("id"));
                item.setIfShow(rs.getString("if_show"));
                item.setShowOrder(rs.getInt("show_order"));
                item.setHousePic(rs.getBytes("house_pic"));
                hiList.add(item);
            }
        }catch (Exception ex)
        {
        }
        return hiList;
    }
}
